﻿using System;
using System.ComponentModel.Composition;
using System.Windows.Forms;
using PefStudio.StartUp;

namespace PefStudio.Windows
{
    public class AbstractMenuItem : ToolStripMenuItem, IMenuItem
    {
        [Import(Bootstrapper.MainWindowContract, typeof(Form))]
        private Lazy<MainWindow> mainWindow { get; set; }
        protected MainWindow MainWindow
        {
            get { return mainWindow.Value; }
        }

        public AbstractMenuItem()
        {
            DropDownOpening += new EventHandler(AbstractMenuItem_DropDownOpening);
            Index = double.MaxValue;
        }

        void AbstractMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            foreach (var item in DropDownItems)
            {
                if (item is IMenuItem)
                {
                    var menuItem = item as IMenuItem;
                    menuItem.ConditionEvaluate();
                }
            }
        }

        public string ID { get; set; }
        public double Index { get; set; }
        public string InsertRelativeToID { get; set; }
        public bool BeforeRelativeID { get; set; }

        public bool HasSeparator { get; set; }
        public ToolStripSeparator ToolStripSeparator { get; set; }

        public Form ParentForm { get; set; }

        public virtual void ConditionEvaluate() { }

        public new bool Visible
        {
            get
            {
                return base.Visible;
            }
            set
            {
                base.Visible = value;
                if (ToolStripSeparator != null)
                    ToolStripSeparator.Visible = value;
            }
        }
    }
}
